package com.logic;

import com.pjc.db.MongoDB;
import com.pjc.db.MySqlConn;
import com.pjc.db.RedisDB;
import com.pjc.thrift.TLogicService;
import com.pjc.utils.ConfigUtil;
import org.apache.thrift.TProcessor;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.protocol.TCompactProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.pjc.utils.SystemUtil;
import com.logic.handler.ProtoHandler;
import com.logic.utils.LogicSrvUtils;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;
import ch.qos.logback.classic.LoggerContext;

/**
 * 逻辑服务器启动入口
 * 
 * @author 王畅
 * @version 创建时间：2015-6-17 下午03:49:12
 */
public class Shell {
	private static Logger log = LoggerFactory.getLogger(Shell.class);
	private static int THRIFT_PORT = 9394;
	private static int thriftPort = 9391;
	private static String redisHost = "";
	private static int redisPort;
	private static int redisDBIndex;
	private static String redisPwd="";

	/**
	 * main方法
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		log.info("初始化日志组件......");
		initLogger();
		log.info("日志组件初始化完成......");
		log.info("初始化config.properties文件......");
		ConfigUtil.getInstance().initConfig(Shell.class);
		initConfig();
		log.info("初始化config.properties文件完成......");
		log.info("扫描协议中......");
		LogicSrvUtils.getInstance().setUp();
		log.info("扫描协议完成......");

		log.info("Thrift 服务正在监听中。监听端口：" + ConfigUtil.getInstance().getIntProperties("thrift.port", THRIFT_PORT) + "。");
		initThriftService();
	}

	/**
	 * 初始化参数信息
	 * 
	 * @throws Exception
	 */
	private static void initConfig() throws Exception {

		thriftPort = ConfigUtil.getInstance().getIntProperties("thrift.port", 9394);

		/********************** 初始化redis数据库 *************************************/
		redisHost = ConfigUtil.getInstance().getStringProperties("db.redis.host");
		redisPort = ConfigUtil.getInstance().getIntProperties("db.redis.port");
		redisDBIndex = ConfigUtil.getInstance().getIntProperties("db.redis.dbindex", 1);
		redisPwd = ConfigUtil.getInstance().getStringProperties("db.redis.password");
		RedisDB.getInstance().init(redisHost, redisPort,redisPwd,redisDBIndex);

		/********************** 初始化mongodb数据库 *************************************/
		String mongoHost = ConfigUtil.getInstance().getStringProperties("db.mongo.host");
		int mongoPort = ConfigUtil.getInstance().getIntProperties("db.mongo.port", 27017);
		String mongoDb = ConfigUtil.getInstance().getStringProperties("db.mongo.dbname");
		String mongoUser = ConfigUtil.getInstance().getStringProperties("db.mongo.user");
		String mongoPwd = ConfigUtil.getInstance().getStringProperties("db.mongo.password");
		MongoDB.init(mongoHost, mongoPort,mongoDb,mongoUser,mongoPwd);

		/********************** 初始化mysql数据库 *************************************/
		String mysqlHost = ConfigUtil.getInstance().getStringProperties("db.mysql.url");
		String mysqlName = ConfigUtil.getInstance().getStringProperties("db.mysql.user");
		String mysqPwd = ConfigUtil.getInstance().getStringProperties("db.mysql.password");
		MySqlConn.getInstance().init(mysqlHost, mysqlName, mysqPwd);

	}

	/**
	 * 启动thrift监听
	 * 
	 * @throws TTransportException
	 */
	public static void initThriftService() throws TTransportException {
		TMultiplexedProcessor tprocessor = new TMultiplexedProcessor();
		TProcessor processor = new TLogicService.Processor<TLogicService.Iface>(new ProtoHandler());
		tprocessor.registerProcessor("logicService", processor);
		TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(thriftPort);
		TNonblockingServer.Args args = new TNonblockingServer.Args(tnbSocketTransport);
		args.processor(tprocessor);
		args.transportFactory(new TFramedTransport.Factory());
		args.protocolFactory(new TCompactProtocol.Factory());
		TServer server = new TNonblockingServer(args);
		server.serve();
	}

	/**
	 * 启动日志服务
	 */
	private static void initLogger() {
		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		JoranConfigurator configurator = new JoranConfigurator();
		configurator.setContext(lc);
		lc.reset();
		try {
			String path = SystemUtil.GetConfigPath(Shell.class, "logback.xml");
			configurator.doConfigure(path);
		} catch (JoranException e) {
			log.error("initLogger", e);
		}
		StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
	}
}
